package com.example.metesting;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

public class EventCreateActivity extends Activity implements OnClickListener,
		TimePickerFragment.OnTimePickedListener,
		DatePickerFragment.OnDatePickedListener, OnCheckedChangeListener {

	private Button bStartDate, bStartTime, bEndDate, bEndTime, bSave, bCreate;
	private CheckBox cbAllDay;
	private EditText et1;
	private Calendar tempCal, startCal, endCal;
	private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
	private SimpleDateFormat dateFormat = new SimpleDateFormat("d/M/yyyy");
	FrameLayout touchInterceptor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL,
//				LayoutParams.FLAG_NOT_TOUCH_MODAL);
//		getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
//				LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
		
		touchInterceptor = new FrameLayout(this);
	    touchInterceptor.setClickable(true);

		setContentView(R.layout.activity_event_create);
	
		// Set up the default start end date time
		startCal = Calendar.getInstance();
		startCal.add(Calendar.HOUR_OF_DAY, 1);
		startCal.set(Calendar.MINUTE, 0);
		endCal = Calendar.getInstance();
		endCal.add(Calendar.HOUR_OF_DAY, 2);
		endCal.set(Calendar.MINUTE, 0);

		bStartDate = (Button) findViewById(R.id.bStartDate);
		bStartTime = (Button) findViewById(R.id.bStartTime);
		bEndDate = (Button) findViewById(R.id.bEndDate);
		bEndTime = (Button) findViewById(R.id.bEndTime);
		bSave = (Button) findViewById(R.id.bSave);
		bCreate = (Button) findViewById(R.id.bCreate);
		cbAllDay = (CheckBox) findViewById(R.id.cbAllDay);
		et1 = (EditText) findViewById(R.id.etTitle);

		bStartDate.setOnClickListener(this);
		bStartTime.setOnClickListener(this);
		bEndDate.setOnClickListener(this);
		bEndTime.setOnClickListener(this);
		bSave.setOnClickListener(this);
		bCreate.setOnClickListener(this);
		cbAllDay.setOnCheckedChangeListener(this);

//		ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
//		
//		clipboard.getPrimaryClip();
//		
//		ClipData abc = clipboard.getPrimaryClip();
//		ClipData.Item item = abc.getItemAt(0);
//		String text = item.getText().toString();
//		et1.setText(text);
		
		updateButtonDateTime();

	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}




	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// If we've received a touch notification that the user has touched
		// outside the app, finish the activity.
		if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
			moveTaskToBack(true);
			return false;
		}
		// Delegate everything else to Activity.
		return super.onTouchEvent(event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bStartDate:
			DialogFragment startDatePicker = DatePickerFragment.newInstance(1,
					startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH),
					startCal.get(Calendar.DAY_OF_MONTH));
			System.out.println(startCal.get(Calendar.MONTH) + " "
					+ startCal.get(Calendar.DAY_OF_MONTH));
			startDatePicker.show(getFragmentManager(), "datePicker");
			break;
		case R.id.bStartTime:
			DialogFragment startTimePicker = TimePickerFragment.newInstance(1,
					startCal.get(Calendar.HOUR_OF_DAY),
					startCal.get(Calendar.MINUTE));
			startTimePicker.show(getFragmentManager(), "timePicker");
			break;
		case R.id.bEndDate:
			DialogFragment endDatePicker = DatePickerFragment.newInstance(2,
					endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH),
					endCal.get(Calendar.DAY_OF_MONTH));
			endDatePicker.show(getFragmentManager(), "datePicker");
			break;
		case R.id.bEndTime:
			DialogFragment endTimePicker = TimePickerFragment.newInstance(2,
					endCal.get(Calendar.HOUR_OF_DAY),
					endCal.get(Calendar.MINUTE));
			endTimePicker.show(getFragmentManager(), "timePicker");
			break;
		case R.id.bSave:
			finish();
			break;
		case R.id.bCreate:
			finish();
			break;
		}
	}

	public void updateButtonDateTime() {
		bStartTime.setText(timeFormat.format(startCal.getTime()));
		bEndTime.setText(timeFormat.format(endCal.getTime()));
		bStartDate.setText(dateFormat.format(startCal.getTime()));
		bEndDate.setText(dateFormat.format(endCal.getTime()));
	}

	public void onTimePicked(int pickerId, int hourOfDay, int minute) {
		System.out.println(hourOfDay + "  " + minute);
		switch (pickerId) {
		case 1:
			tempCal = (Calendar) startCal.clone();
			int hourDiff = endCal.get(Calendar.HOUR_OF_DAY)
					- tempCal.get(Calendar.HOUR_OF_DAY);
			startCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
			startCal.set(Calendar.MINUTE, minute);
			
			if (startCal.after(endCal)) {
				endCal = (Calendar) startCal.clone();
				endCal.add(Calendar.HOUR_OF_DAY, hourDiff);
			}
			updateButtonDateTime();
			break;
		case 2:
			tempCal = (Calendar) endCal.clone();
			endCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
			endCal.set(Calendar.MINUTE, minute);
			if (startCal.after(endCal)) {
				Toast.makeText(this,
						"End Date should be later than Start Date!",
						Toast.LENGTH_SHORT).show();
				endCal = (Calendar) tempCal.clone();
			} else {
				updateButtonDateTime();
			}

			break;
		}
	}

	@Override
	public void onDatePicked(int pickerId, int year, int month, int day) {
		// TODO Auto-generated method stub

		switch (pickerId) {
		case 1:
			startCal.set(Calendar.YEAR, year);
			startCal.set(Calendar.MONTH, month);
			startCal.set(Calendar.DAY_OF_MONTH, day);
			if (startCal.after(endCal)) {
				endCal = (Calendar) startCal.clone();
				endCal.add(Calendar.HOUR_OF_DAY, 1);
			}
			updateButtonDateTime();
			break;
		case 2:
			tempCal = (Calendar) endCal.clone();
			endCal.set(Calendar.YEAR, year);
			endCal.set(Calendar.MONTH, month);
			endCal.set(Calendar.DAY_OF_MONTH, day);
			if (startCal.after(endCal)) {
				Toast.makeText(this,
						"End Date should be later than Start Date!",
						Toast.LENGTH_SHORT).show();
				endCal = (Calendar) tempCal.clone();
			} else {
				updateButtonDateTime();
			}
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (buttonView.isChecked()) {
			bStartTime.setVisibility(View.GONE);
			bEndTime.setVisibility(View.GONE);
		} else {
			bStartTime.setVisibility(View.VISIBLE);
			bEndTime.setVisibility(View.VISIBLE);
		}
	}

}
